
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_jobpurchase : System.Web.UI.Page
{
     
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    try
    {
        if(Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;

        
        if (!IsPostBack)
        {
             
            ViewState["SortDirection"] = "ASC";
            ShowPackage();
            
        }
        if (Session["rcactivate"].ToString() == "False")
        {
            btnSave.Enabled = false;

            litError.Text = "<img border=0 src='/icons/warning.gif'>You can not purchase RCV Jobs and Viewing services, <br>because this account is not approved by management team.";
        } 
       }
        catch(Exception ex)
        {
            litError.Text = ex.Message;
            
        }
    }
  
    
    DataTable GetDataTable()
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;
        sqlServerProvider.CommandText = "select * from vwSalesPackages";        
        DataTable dataTable = sqlServerProvider.GetDataTable();
        return dataTable;
    }
    DataView GetDataView(string columnName, string sortType)
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;
        sqlServerProvider.CommandText = "select * from vwSalesPackages";         
        DataTable dataTable = sqlServerProvider.GetDataTable();
        DataView dataView = new DataView(dataTable);
        dataView.Sort = columnName + " " + sortType;
        return dataView;
    }
 
    void ShowPackage()
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(
            GetDataTable(),
            this.GridView1);  
            if (GridView1.Rows.Count == 0)
            {
                litError.Text = "Package not found.";
                 
            }
            else
            {
                FillValidate();
                if (GridView1.Rows.Count < 10)
                    btnSave.Visible = true;     
                else
                    btnSave.Visible = false;                                
                litError.Text = "";
                 
            }
             
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    void ShowPackage(string columnName, string sortType)
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataViewToGridView(
            GetDataView(columnName, sortType),
            this.GridView1); ;
            
            if (GridView1.Rows.Count == 0)
                litError.Text = "Package not found.";
            else
            {
                FillValidate();
                litError.Text = "";
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }

    void FillValidate()
    {
        foreach (GridViewRow gridViewRow
         in GridView1.Rows)
        {
            RangeValidator rangeValidator = (RangeValidator)
            gridViewRow.FindControl("rv");
            rangeValidator.MinimumValue = gridViewRow.Cells[8].Text;
            rangeValidator.MaximumValue = gridViewRow.Cells[9].Text;
            rangeValidator.Type = ValidationDataType.Integer;
            rangeValidator.Text = "Value: " + gridViewRow.Cells[8].Text + "-" + gridViewRow.Cells[9].Text; 
            
        }  
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ShowPackage(
        e.SortExpression, ViewState["SortDirection"].ToString());
        if (ViewState["SortDirection"].ToString() == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
    }
     
     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gridViewRow = e.Row;
        gridViewRow.Cells[8].Visible = false;
        gridViewRow.Cells[9].Visible = false;
        gridViewRow.Cells[10].Visible = false;
    }
    protected void btnCalculate_Click(object sender, EventArgs e)
    {
        int totalJobAmount = 0, totalViewAmount = 0;
        int totalJobs = 0, totalViews = 0;
        int totalDiscount = 0;
        foreach (GridViewRow gridViewRow
                 in GridView1.Rows)
        {
            TextBox textBox = (TextBox)
            gridViewRow.FindControl("txtPurchase");

            if (textBox.Text != "")
            {
                if (gridViewRow.Cells[10].Text == "False")
                {
                    totalJobs += Convert.ToInt32(textBox.Text);
                    totalJobAmount += Convert.ToInt32(gridViewRow.Cells[4].Text) * Convert.ToInt32(textBox.Text);
                    if (gridViewRow.Cells[5].Text != "")
                         totalDiscount += totalJobAmount * Convert.ToInt32(gridViewRow.Cells[5].Text) / 100;
                }
                else
                {
                    totalViews += Convert.ToInt32(textBox.Text);
                    totalViewAmount += Convert.ToInt32(gridViewRow.Cells[4].Text) * Convert.ToInt32(textBox.Text);
                    if (gridViewRow.Cells[5].Text != "")
                        totalDiscount -= totalDiscount * Convert.ToInt32(gridViewRow.Cells[5].Text) / 100; 
                }
            }

        } 
        totalViewAmount -= totalDiscount;
        literalJobs.Text =   totalJobs.ToString();
        literalViews.Text = totalViews.ToString();
        literalDiscount.Text = totalDiscount.ToString("#,###.00");
        literalAmount.Text = (totalJobAmount + totalViewAmount).ToString("#,###.00");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       try
           {   
            if (ddlPaymentBy.SelectedValue =="Cash"
            || ddlPaymentBy.SelectedValue == "Bank Transfer"
            || ddlPaymentBy.SelectedValue == "ATM Transfer")
            {
            RecruitVietnam recruitVietnam = new RecruitVietnam();
            recruitVietnam.ConnectionString = connectionString;
            int packageID = recruitVietnam.SaveSalesPackage(
            Session["rcuserid"].ToString(), 
            txtContactName.Text, 
            ddlPaymentBy.SelectedValue, 
            ddlCurrency.SelectedValue);
            int totalJobs = 0, totalViews = 0;
            string insertQuery = "";
            foreach (GridViewRow gridViewRow
                 in GridView1.Rows)
            {
                TextBox textBox = (TextBox)
                gridViewRow.FindControl("txtPurchase");

                if (textBox.Text != "")
                {
                    insertQuery += 
                    "insert into tblSalesPackageDetails" +
                    " values('" + packageID.ToString() + "','" 
                    + gridViewRow.Cells[2].Text +
                    "','" + textBox.Text  + "','" +  
                    gridViewRow.Cells[4].Text  + "','" +
                    ((gridViewRow.Cells[10].Text == "False")?"0":"1") + "');";                         
                    if (gridViewRow.Cells[10].Text == "False")                
                        totalJobs += Convert.ToInt32(textBox.Text);
                    else
                        totalViews += Convert.ToInt32(textBox.Text);
                }
           }
          if (insertQuery != "")
          {
            recruitVietnam.ExecuteSQL(insertQuery);                
            Response.Redirect("jobpurchases.aspx");
          } 
          else
              litError.Text = "Please enter jobs or view number.";
          }
          else
              litError.Text = "Payment method is not supported";
        }
        catch(Exception ex) 
        {
            litError.Text = ex.Message;
        }
    }
}

